package com.match.competition_role.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.match.competition_role.client.RoleClient;
import com.match.competition_role.entity.CompetitionRole;
import com.match.competition_role.service.CompetitionPermissionService;
import com.match.competition_role.service.CompetitionRoleService;
import com.match.competition_role.service.LoginService;
import com.match.entity.baseEntity.Student;
import com.match.entity.baseEntity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 芊薰
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private RoleClient roleClient;
    @Autowired
    private CompetitionRoleService competitionRoleService;
    @Autowired
    private CompetitionPermissionService competitionPermissionService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据职工号获取用户信息
     * @param number
     * @return
     */
    @Override
    public Map<String, Object> getUserInfo(String number) {
        Map<String,Object> map=new HashMap<>();
        //获取用户信息
        Object teaInfo = roleClient.getTeaByNum(number).getData().get("teaInfo");
        String s = JSON.toJSONString(teaInfo);
        Teacher teacher = JSON.parseObject(s, Teacher.class);
        if (!StringUtils.isEmpty(teacher)){
            //获取用户角色信息
            List<CompetitionRole> roleList=competitionRoleService.getUserRoleByNumber(number);
            List<String> roleNameList=roleList.stream().map(item -> item.getRoleName()).collect(Collectors.toList());
            if (roleNameList.size()==0){
                //前端框架必须返回一个角色，否则报错，如果没有角色，返回一个空角色
                roleNameList.add("");
            }
            //根据用户number获取操作权限值
            List<String> permissionValueList = competitionPermissionService.selectPermissionValueByTeacherNumber(number);
            redisTemplate.opsForValue().set(number, permissionValueList);
            map.put("name", teacher.getName());
            map.put("avatar", teacher.getAvatar());
            map.put("roles", roleNameList);
            map.put("number",teacher.getNumber());
            map.put("permissionValueList", permissionValueList);
        }else {
            //获取用户信息
            //根据学号查询学生
            Object stuInfo = roleClient.findStuByNum(number).getData().get("stuInfo");
            String string = JSON.toJSONString(stuInfo);
            Student student = JSON.parseObject(string, Student.class);
            if (!StringUtils.isEmpty(student)){
                map.put("name", student.getName());
                map.put("avatar", student.getAvatar());
                map.put("roles", "");
                map.put("number",student.getNumber());
                map.put("permissionValueList", "");
            }
        }
        return map;
    }


    /**
     * 获取权限
     * @param number
     * @return
     */
    @Override
    public List<JSONObject> getPermission(String number) {
        //根据用户id获取用户菜单权限
        List<JSONObject> permissionList = competitionPermissionService.selectPermissionByUserNumber(number);
        return permissionList;
    }
}
